<script lang="ts" setup>
  import { timeFormathmsUTC } from '@/utils/formatter'
  import { onUnmounted } from 'vue'
  const props = defineProps({
    data: Object,
    title: String
  })
  const emit = defineEmits(['close'])
  const dictionary: { [key: string]: string } = {
    instance_id: '实例ID',
    process_id: '进程ID',
    server_session_id: '会话ID',
    name: '名称',
    instance_name: '名称',
    health_status: '健康状态',
    created_at: '创建时间',
    updated_at: '更新时间',
    process_count: '进程数量',
    ip_address: '公网IP',
    server_session_count: '会话数量',
    state: '状态',
    max_server_session_num: '最大会话数量',
    port: '端口',
    current_client_session_count: '客户端会话数量',
    creator_id: '创建者ID',
    state_reason: '状态原因',
    fleet_id: 'FleetID',
    session_data: '会话数据',
    session_properties: '会话属性',
    client_session_creating_policy: '客户端会话创建策略',
    max_client_session_num: '最大客户端会话数量',
    server_session_protection_policy: '会话保护策略',
    server_session_protection_time_limit_minutes: '会话保护时间',
    created_time: '创建时间',
    updated_time: '更新时间',
    active_at: '激活时间'
  }
  const session = {
    key: '键',
    value: '值'
  }
  const bodyClose = (e: any) => {
    if (e.target.className === 'dialog') {
      emit('close')
    }
  }
  document.addEventListener('click', bodyClose)
  onUnmounted(() => {
    document.removeEventListener('click', bodyClose)
  })
</script>
<template>
  <div id="info-dialog" class="dialog">
    <div class="dialog-wrap">
      <div class="dialog-header">
        <span class="heder-title">{{ props.title }}详情</span>
        <el-icon><component is="Close" @click="emit('close')" /></el-icon>
      </div>
      <div class="dialog-content">
        <div class="warp">
          <div class="data-box" v-for="(item, key) in props.data" :key="key">
            <div class="label">
              <span>{{ dictionary[key] ? dictionary[key] : key }}</span>
            </div>
            <div class="name-value" v-if="['created_at', 'created_time', 'updated_time', 'updated_at', 'active_at'].includes(key)">
              {{ timeFormathmsUTC(item) }}
            </div>
            <div class="name-value" v-else-if="key == 'session_properties'">
              <div class="card-table">
                <div class="content-table">
                  <div class="table-title" v-for="p in session" :key="p">{{ p }}</div>
                </div>
                <div class="content-table-body">
                  <div class="body-table" v-for="(t, index) in item" :key="index">
                    <div v-for="(p, key) in session" :key="key" class="table-title">{{ t[key] }}</div>
                  </div>
                </div>
              </div>
            </div>
            <div class="name-value" v-else>{{ $t(item) }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
  #info-dialog {
    z-index: 20;
  }
  .dialog {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.4);
    overflow: auto;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .dialog-wrap {
    border-radius: 4px;
    background-color: #fff;
  }
  .dialog-header {
    padding: 15px 26px;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid $border-color;
    .el-icon {
      cursor: pointer;
    }
  }
  ::-webkit-scrollbar {
    width: 8px !important;
  }
  ::-webkit-scrollbar-thumb {
    background-color: #e1e3e4 !important;
  }
  ::-webkit-scrollbar-track-piece {
    background: #fff !important;
  }

  .dialog {
    justify-content: flex-end;
  }

  .dialog-header {
    padding: 17px 40px;

    .heder-title {
      font-size: 18px;
      color: #1d1d1f;
      font-weight: bold;
    }
  }

  .warp {
    padding: 0 40px;
  }

  .data-box {
    width: 100%;
    margin-bottom: 24px;
    display: flex;
    align-items: center;

    .label {
      display: inline-block;
      flex-shrink: 0;
      font-size: 14px;
      color: $action-color;
      line-height: 30px;
      height: 30px;
      margin-right: 20px;
      text-align: left;
      white-space: nowrap;

      span {
        display: inline-block;
        width: 126px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .name-value {
      width: 480px;
      min-height: 30px;
      line-height: 30px;
      color: $text-color;
    }
  }

  .dialog-wrap {
    width: 800px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    border-radius: 0;
  }

  .dialog-content {
    width: 100%;
    margin: 0 auto;
    padding: 30px 0 0 0;
    flex: 1;
    overflow-y: scroll;

    &::-webkit-scrollbar-thumb {
      background-color: #e1e3e4;
    }

    &::-webkit-scrollbar-track-piece {
      background: #fff;
    }
    .card-table {
      padding: 0 10px;
      border: 1px solid $border-color;
      border-radius: 2px;
      font-size: 14px;
      .content-table {
        display: flex;
        align-items: center;
        height: 34px;
        border-bottom: 1px solid $border-color;
        color: $action-color;
      }
      .body-table {
        height: 50px;
        display: flex;
        align-items: center;
        border-bottom: 1px solid $border-color;
        color: $text-color;
      }
      .table-title:first-child {
        width: 550px;
      }
      .table-title:nth-child(2) {
        width: 560px;
      }
      .table-title:nth-child(3) {
        width: 230px;
      }
      .body-table:last-child {
        border: none;
      }
    }
  }
</style>
